/**
 * 消息公告页面（admin）
 */
var noticeUI = function() {
    var self = this;
    PageList.call(self);

    self.table = null;
    self.allInput = null;
    self.userInput = null;
    self.typeInput = null;
};
noticeUI.prototype = Object.create(PageList.prototype);

noticeUI.prototype.init = function() {
    var self = this;
    self.refresh(1);
    self.jumpPage = function(page) {
        self.refresh(page);
    };
}

noticeUI.prototype.refresh = function(page) {
    var self = this;
    var all = self.allInput.val();
    var uid = undefined;
    if (all == '0') {
        uid = '';
    }
    else {
        if (self.userInput != null && self.userInput.val().trim() != '') {
            uid = self.userInput.val().trim();
        }
    }
    var type = self.typeInput.val();
    if (type == '-1') type = null;

    CMD.notice_query(uid, type, null, self.getPageSize(), page, function(r) {
        self.fillResults(r);
    });
}

noticeUI.prototype.fillResults = function(r) {
    var self = this;
    self.list = r.list;
    self.fillPages(r.count, r.number, r.page, r.size);

    self.table.empty();
    for (var i in r.list) {
        var d = r.list[i];
        var tr = $("<tr></tr>");
        tr.append('<td>' + (d.user.id ? d.user.mobile : ' ') + '</td>');
        tr.append('<td>' + d.subject + '</td>');
        tr.append('<td class="detail" value="' + i + '">' + d.content + '</td>');
        tr.append('<td>' + (d.type || '') + '</td>');
        if (Rights.check("/notice/delete", context.user.user.grade || '1'))
            tr.append('<td class="oper"><button value="' + i + '">删除</button></td>');
        else
            tr.append('<td> </td>');
        self.table.append(tr);
    }

    var arr = $(".table td.detail");
    for (var i = 0; i < arr.length; i++)
        self.registerPopover($(arr[i]));

    $(".oper button").click(function(e) {
        var index = $(e.currentTarget).attr("value");
        var data = self.list[index];
        startLoading();
        CMD.notice_delete(data.id, function() {
            stopLoading();
            self.refresh(self.currPage);
        });
    });
}

noticeUI.prototype.registerPopover = function(td) {
}

// 消息添加框
noticeUI.prototype.showForm = function() {
    Util.addPopUp("add_notice", "add_notice", 500);
}

window.NoticeUI = new noticeUI();
